package com.engine.budget.cmd;


import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 祖梦杰
 * @date 2024/7/23 10:37
 * @description:重点工作查询接口
 * @version 1.0
 */
public class GetFocusWork extends AbstractCommonCommand<Map<String,Object>> {

    public GetFocusWork(User user, Map<String,Object> params) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {


        Map<String, Object> map = new HashMap<>();
        //4.定义SQL语句
        String sql="SELECT dwzdgzbh unitWorkNum,dwzdgzmc unitWorkName,dwzdgzms unitWorkRemake,szgszdgz belongUnitWork,sydw apaplyWork FROM uf_dwzdgz WHERE  1 = 1 ";

        String sqlTotal="SELECT COUNT(1) total FROM uf_dwzdgz WHERE 1 = 1 ";

        ArrayList<String> sqlArrs = new ArrayList<>();
        ArrayList<String> sqlTotalArrs = new ArrayList<>();
        String unitWorkNum  = (String)params.get("unitWorkNum");
        if(unitWorkNum !=null&&!unitWorkNum .isEmpty()){
            sql+=("  AND dwzdgzbh = ? " );
            sqlTotal+=("  AND dwzdgzbh = ? " );
            sqlArrs.add(unitWorkNum);
            sqlTotalArrs.add(unitWorkNum);
        }


        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sqlTotal,sqlTotalArrs);
        while (recordSet.next()) {
            map.put("total",recordSet.getString("total"));
        }


        int pageIndex = Util.getIntValue(params.get("pageIndex").toString(), 0);
        int pageSize = Util.getIntValue(params.get("pageSize").toString(), 0);
        if(pageIndex!=0&&pageSize!=0){
            sql+="LIMIT "+pageSize+" OFFSET "+((pageIndex-1)*pageSize);
        }else {
            sql+=("LIMIT 10 OFFSET 0 ");
        }



        RecordSet rs = new RecordSet();
        rs.executeQuery(sql,sqlArrs);
        ArrayList<Map> data = new ArrayList<>();
        //7.处理结果，并获取数据
        while (rs.next()){
            Map<String, Object> mainMap = new HashMap<>();
            mainMap.put("unitWorkNum",rs.getString("unitWorkNum"));
            mainMap.put("unitWorkName",rs.getString("unitWorkName"));
            mainMap.put("unitWorkRemake",rs.getString("unitWorkRemake"));
            mainMap.put("belongUnitWork",rs.getString("belongUnitWork"));
            mainMap.put("apaplyWork",rs.getString("apaplyWork"));
            data.add(mainMap);
        }
        map.put("data",data);
        return map;
    }

}
